/*
Copyright (c) 2008 SmashedApples 

Contributors.  See: http://smashedapples.googlecode.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package smashedapples.core
{	
	
	import flash.events.EventDispatcher;
	
	import mx.containers.Form;
	
	import smashedapples.events.ValueObjectEvent;
	import smashedapples.reflection.ReflectionManager;
	import smashedapples.reflection.metadata.PropertyMD;
	import smashedapples.reflection.metadata.ValueObjectMD;
	import smashedapples.utils.FormUtils;
	
	[Event(name="setEvent", type="smashedapples.events.ValueObjectEvent")]
	
	[Bindable]
	dynamic public class ValueObject extends EventDispatcher
	{
		public function ValueObject(args:Object=null):void
		{
			setVO(args);
		}

		public function setVO(args:*=null):void
        {    

            if (args is XML)
                setFromXML( args ); 
            else if( args is Form)
                setFromForm(  args as Form );
            else 
            {
                setFromObject(args);
            } 
            
            this.dispatchEvent(new ValueObjectEvent("setEvent"));
        }
        
        public function setFromForm(form:Form=null):void
        {
            if(form == null)
                return void;
                
            var args : Object = FormUtils.getFormVales(form);
            setFromObject(args);
    
        }
        
       
        public function setFromObject(object:Object=null):void
        {
            if(object == null)
                return void;
            
  			//trace(this.metadata);
            
            for (var key:String in object)
            {	
  				var metadata : PropertyMD = (this.metadata.properties[key] as PropertyMD);
  				
  				switch(metadata.type)
  				{
  					case "String":
  						this[key] = object[key] as String;
  					break;
  					case "Number":
  						this[key] = object[key] as Number;
  					break;
  					case "Date":
  						this[key] = object[key] as Date;
  					break;
  					case "Array":
  						this[key] = object[key] as Array;
  					break;
  					
  				}
  				
                    
            } 
                
        }
        
        public function setFromXML(xml:XML=null):void
        {    
            if(xml == null)
                return void;
            
            var nodes : XMLList = xml.children();
            for each (var node:XML in nodes)
            {    
                this[ String(node.name())] =  node.valueOf();        
            }
        }
		
		
		
	 
		[Transient]
		private var _metadata : ValueObjectMD;
		[Transient]
		public function get metadata():ValueObjectMD
		{	
			if(_metadata == null)
				_metadata = ReflectionManager.getVOMetaData( this );
			return _metadata;
		} 
		
	
		/* private var _itemID : String;
		public function get itemID():String
		{
			return _itemID;
		}
		public function set itemID(value:String):void
		{
			_itemID = value;
		}
		 */
	
	
	}
}